package org.lex.notepad.ui.preferences;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JCheckBox;

import org.lex.swing.ActionBuilder;
import org.lex.swing.prefs.CategoryAdapter;


public class TestCategory extends CategoryAdapter implements ActionListener {
	private JCheckBox checkBox = null;

	public String getTitle() {
		return "Test";
	}

	public TestCategory() {
		super();
		this.setLayout(new FlowLayout());
		this.add(getCheckBox());
		this.fireContentChanged(false);
	}

	private JCheckBox getCheckBox() {
		if (null == checkBox) {
			checkBox = new JCheckBox(new ActionBuilder("Not selected").setTooltip("Tool Tip").setActionListener(this));
		}
		return checkBox;
	}

	private Font dialog = new Font("Dialog", Font.PLAIN, 12);
	private Font song = new Font("����", Font.PLAIN, 14);

	public void actionPerformed(ActionEvent e) {
		if (true == checkBox.isSelected()) {
			checkBox.setText("Selected");
			this.setAllFont(song);
		} else {
			checkBox.setText("Not Selected");
			this.setAllFont(dialog);
		}
	}

	private void setAllFont(Font font) {
		Container root = checkBox;
		// find root
		while (true) {
			Container parent = root.getParent();
			if (null == parent)
				break;
			else
				root = parent;
		}
		System.out.println(root.getClass().getName());
		// set font
		Stack<Container> stack = new Stack<Container>();
		stack.push(root);
		while (false == stack.isEmpty()) {
			Container c = stack.pop();
			Component[] children = c.getComponents();
			for (Component com : children) {
				com.setFont(font);
				if (com instanceof Container)
					stack.push((Container) com);
				System.out.println(com.getClass().getName());
			}
		}
	}
}
